﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using XMK.Tadong.Model;
using XMK.Tadong.Biz;
using XMK.Tadong.Web.Language;

namespace XMK.Tadong.Web.WebFileExplorer.DataSubmit
{
    public partial class EditSourceFiles : FileExplorerPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindLANG_TYPE();
                BindSECRET_LVL();
                BindData();
            }
        }

        //提交按钮点击事件
        protected void btnSunmit_Click(object sender, EventArgs e)
        {
            try
            {
                string File_ID = string.Empty;
                if (!string.IsNullOrEmpty(hidFile_ID.Value))
                {
                    File_ID = hidFile_ID.Value;
                }

                string spacetype = "";
                string[] spacetypes = new string[3];
                if (!string.IsNullOrEmpty(this.Request.QueryString["spacetype"].Trim()))
                {
                    spacetype = this.Request.QueryString["spacetype"];
                    spacetypes = spacetype.Split(',');
                   
                }
                if (spacetypes[0].Equals("individual"))
                {
                    
                    TB_PERSON_FILEInfo model = new TB_PERSON_FILEInfo();
                    TB_PERSON_FILEBiz bll = new TB_PERSON_FILEBiz();
                    model.FILE_ID = Convert.ToDecimal(File_ID);
                    model.FILE_NAME = txtWDMC.Text.Trim();
                    model.FILE_AUTHOR = txtZZ.Text.Trim();
                    model.KEYW = hidGJC.Value.Trim();
                    model.FILE_ABST = hidZYXX.Value.Trim();
                    model.FILE_CRT_DATE = Convert.ToDateTime(paydate.Value.Trim());
                    if (!txtMB.Text.Trim().Equals(string.Empty)) model.DEST_OBJ = txtMB.Text.Trim();
                    if (!txtYWDX.Text.Trim().Equals(string.Empty)) model.BIZ_OBJ = txtYWDX.Text.Trim();
                    model.UPD_TIME = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                    if (!ddlstWDGS.SelectedValue.Trim().Equals(string.Empty)) model.FILE_EXT = ddlstWDGS.SelectedValue.Trim();
                    //后添加的属性
                    //语种
                    if (!ddlstLANG_TYPE.SelectedValue.Trim().Equals(string.Empty)) model.LANG_TYPE = Convert.ToDecimal(ddlstLANG_TYPE.SelectedValue.Trim());
                    //版本
                    //model.FILE_VERSION = txtBB.Text.Trim();
                    if (!txtBB.Text.Trim().Equals(string.Empty)) model.FILE_VERSION = Convert.ToDecimal(txtBB.Text.Trim());
                    //密级
                    if (!ddlstSECRET_LVL.SelectedValue.Trim().Equals(string.Empty)) model.SECRET_LVL = ddlstSECRET_LVL.SelectedValue.Trim();
                    if (bll.UpDateSourceData(model))
                    {
                        lblError.Text = "修改成功";
                        //ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>parent.filemanager.load_MySubmitData(" + Session["user_id"].ToString() + ");</script>");
                    }
                   
                }
                else if (spacetypes[0].Equals("'group"))
                {
                    
                    TB_TEAM_FILEInfo model = new TB_TEAM_FILEInfo();
                    TB_TEAM_FILEBiz bll = new TB_TEAM_FILEBiz();
                    model.FILE_ID = Convert.ToInt32(File_ID);
                    model.FILE_NAME = txtWDMC.Text.Trim();
                    model.FILE_AUTHOR = txtZZ.Text.Trim();
                    model.KEYW = hidGJC.Value.Trim();
                    model.FILE_ABST = hidZYXX.Value.Trim();
                    model.FILE_CRT_TIME = Convert.ToDateTime(paydate.Value.Trim());
                    if (!txtMB.Text.Trim().Equals(string.Empty)) model.DEST_OBJ = txtMB.Text.Trim();
                    if (!txtYWDX.Text.Trim().Equals(string.Empty)) model.BIZ_OBJ = txtYWDX.Text.Trim();
                    model.UPD_TIME = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                    if (!ddlstWDGS.SelectedValue.Trim().Equals(string.Empty)) model.FILE_EXT =ddlstWDGS.SelectedValue.Trim();

                    //后添加的属性
                    //语种
                    if (!ddlstLANG_TYPE.SelectedValue.Trim().Equals(string.Empty)) model.LANG_TYPE = Convert.ToDecimal(ddlstLANG_TYPE.SelectedValue.Trim());
                    //版本
                    if (!txtBB.Text.Trim().Equals(string.Empty)) model.FILE_VERSION = Convert.ToInt32(txtBB.Text.Trim());
                    //密级
                    if (!ddlstSECRET_LVL.SelectedValue.Trim().Equals(string.Empty)) model.SECRET_LVL = ddlstSECRET_LVL.SelectedValue.Trim();
                    if (bll.UpDateSourceData(model))
                    {
                        lblError.Text = "修改成功";
                        //ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>parent.filemanager.load_MySubmitData(" + Session["user_id"].ToString() + ");</script>");
                    }
                   
                    
                }
                else if (spacetypes[0].Equals("project"))
                {
                   
                    TB_XMK_FILEInfo model = new TB_XMK_FILEInfo();
                    Tb_Xmk_FileBiz bll = new Tb_Xmk_FileBiz();
                    model.FILE_ID = Convert.ToDecimal(File_ID);
                    model.FILE_NAME = txtWDMC.Text.Trim();
                    model.FILE_AUTHOR = txtZZ.Text.Trim();
                    model.KEYW = hidGJC.Value.Trim();
                    model.FILE_ABST = hidZYXX.Value.Trim();
                    model.FILE_CRT_DATE = Convert.ToDateTime(paydate.Value.Trim());
                    if (!txtMB.Text.Trim().Equals(string.Empty)) model.DEST_OBJ = txtMB.Text.Trim();
                    if (!txtYWDX.Text.Trim().Equals(string.Empty)) model.BIZ_OBJ = txtYWDX.Text.Trim();
                    model.UPD_TIME = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                    if (!ddlstWDGS.SelectedValue.Trim().Equals(string.Empty)) model.FILE_EXT = ddlstWDGS.SelectedValue.Trim();


                    //后添加的属性
                    //语种
                    if (!ddlstLANG_TYPE.SelectedValue.Trim().Equals(string.Empty)) model.LANG_TYPE = Convert.ToDecimal(ddlstLANG_TYPE.SelectedValue.Trim());
                    //版本
                    model.FILE_VERSION = 1;//txtBB.Text.Trim();
                    //密级
                    if (!ddlstSECRET_LVL.SelectedValue.Trim().Equals(string.Empty)) model.SECRET_LVL = ddlstSECRET_LVL.SelectedValue.Trim();
                    if (bll.UpDateSourceData(model))
                    {
                        lblError.Text = "修改成功";
                    }
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }

        //子页面调用该按钮的点击事件
        protected void button1_Click(object sender, EventArgs e)
        {
            BindData();
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>alert('测试');</script>");
        }

        #region 函数集

        public void BindData()
        {
            string File_ID = string.Empty;
            string User_ID = Session["user_id"].ToString();
            if (!string.IsNullOrEmpty(this.Request.QueryString["File_ID"]))
            {
                File_ID = this.Request.QueryString["File_ID"].ToString();
            }
            string FileName = "";
            string SavePath = "";
            if (!string.IsNullOrEmpty(this.Request.QueryString["path"]))
            {
                SavePath = this.Request.QueryString["path"].ToString();
            }
            //获取文档名称
            if (!string.IsNullOrEmpty(this.Request.QueryString["filename"]))
            {
                txtWDMC.Text = this.Request.QueryString["filename"].ToString();
                FileName = this.Request.QueryString["filename"].ToString();
            }
            string spacetype = "";
            string[] spacetypes = new string[3];
            //课题组提交时，当前课题组的ID
            string Team_ID = "";
            if (!string.IsNullOrEmpty(this.Request.QueryString["spacetype"].Trim()))
            {
                spacetype = this.Request.QueryString["spacetype"];
                spacetypes = spacetype.Split(',');
                if (spacetypes.Length >= 3)
                {
                    Team_ID = spacetypes[1];
                }
            }
             string DiskPath = ConfigurationManager.AppSettings["DiskPath"];
            SavePath = SavePath.Replace(DiskPath, "");
            //string RequestName = this.Request.QueryString["filename"];

            SavePath = SavePath.Replace("/" + FileName, "");
            if (spacetypes[0].Equals("individual"))
            {
                hidspacetype.Value = "individual";
                TB_PERSON_FILEInfo model = new TB_PERSON_FILEInfo();
                TB_PERSON_FILEBiz bll = new TB_PERSON_FILEBiz();

                //model = bll.GetFileInfoById(File_ID);
                model = bll.GetFileInfoById(SavePath);
                if (model!=null)
                {
                    hidFile_ID.Value = model.FILE_ID.ToString();
                    txtWDMC.Text = model.FILE_NAME;
                    hidZYXX.Value = model.FILE_ABST;
                    
                    if (model.FILE_ABST.Length >= 10)
                    {
                        lblZYXX.Text = model.FILE_ABST.Substring(0, 10) + " ...";
                    }
                    else
                    {
                        lblZYXX.Text = model.FILE_ABST;
                    }
                    hidGJC.Value = model.KEYW.ToString();

                    if (model.KEYW.Length >= 10)
                    {
                        lblGJC.Text = model.KEYW.Substring(0, 10) + " ...";
                    }
                    else
                    {
                        lblGJC.Text = model.KEYW.ToString();
                    }
                    txtZZ.Text = model.FILE_AUTHOR;
                    paydate.Value = Convert.ToDateTime(model.FILE_CRT_DATE).ToString("yyyy-MM-dd");
                    txtMB.Text = model.DEST_OBJ;
                    targetValue.Value = model.DEST_OBJ.ToString();
                    txtYWDX.Text = model.BIZ_OBJ;
                    bizValue.Value = model.BIZ_OBJ.ToString();
                    ddlstWDGS.SelectedValue = model.FILE_EXT.ToString();
                    //后添加六个属性
                    //语种
                   // if (model.LANG_TYPE == null)
                        //ddlstLANG_TYPE.SelectedValue = "中文";
                    //else
                    //if (model.LANG_TYPE=="")
                    //    ddlstLANG_TYPE.SelectedValue = "1";
                    //else
                        ddlstLANG_TYPE.SelectedValue = model.LANG_TYPE.ToString();
                    //版本
                    if (!txtBB.Text.Trim().Equals(string.Empty)) model.FILE_VERSION = Convert.ToDecimal(txtBB.Text.Trim());
                    //上传时间
                    if (model.CREATE_TIME!=null)
                        lblFILE_CRT_DATE.Text = Convert.ToDateTime(model.CREATE_TIME).ToString("yyyy-MM-dd"); 
                    //文件大小
                    txtFILE_SIZE.Text = model.FILE_SIZE;
                    //密级
                    if (model.SECRET_LVL == "")
                        ddlstSECRET_LVL.SelectedValue = "1";
                    else
                        ddlstSECRET_LVL.SelectedValue = model.SECRET_LVL;
                    lblteamname.Visible = false;
                    lblTeam.Visible = false;
                }
            }
            else if (spacetypes[0].Equals("'group"))
            {
                hidspacetype.Value = "group";
                TB_TEAM_FILEInfo model = new TB_TEAM_FILEInfo();
                TB_TEAM_FILEBiz bll = new TB_TEAM_FILEBiz();

                //DataTable dt = bll.GetModel(File_ID);
                DataTable dt = bll.GetModel(SavePath);
                if (dt.Rows.Count > 0)
                {
                    hidFile_ID.Value = dt.Rows[0]["File_ID"].ToString();
                    txtWDMC.Text = dt.Rows[0]["FILE_NAME"].ToString();
                    hidZYXX.Value = dt.Rows[0]["FILE_ABST"].ToString(); ;

                    if (dt.Rows[0]["FILE_ABST"].ToString().Length >= 10)
                    {
                        lblZYXX.Text = dt.Rows[0]["FILE_ABST"].ToString().Substring(0, 10) + " ...";
                    }
                    else
                    {
                        lblZYXX.Text = dt.Rows[0]["FILE_ABST"].ToString();
                    }
                    txtZZ.Text = dt.Rows[0]["FILE_AUTHOR"].ToString();
                    hidGJC.Value = dt.Rows[0]["KEYW"].ToString();

                    if (dt.Rows[0]["KEYW"].ToString().Length >= 10)
                    {
                        lblGJC.Text = dt.Rows[0]["KEYW"].ToString().Substring(0, 10) + " ...";
                    }
                    else
                    {
                        lblGJC.Text = dt.Rows[0]["KEYW"].ToString();
                    }
                    paydate.Value = Convert.ToDateTime(dt.Rows[0]["FILE_CRT_TIME"].ToString()).ToString("yyyy-MM-dd");
                    txtMB.Text = dt.Rows[0]["DEST_OBJ"].ToString();
                    targetValue.Value = dt.Rows[0]["DEST_OBJ"].ToString();
                    txtYWDX.Text = dt.Rows[0]["BIZ_OBJ"].ToString();
                    bizValue.Value = dt.Rows[0]["BIZ_OBJ"].ToString();
                    ddlstWDGS.SelectedValue = dt.Rows[0]["FILE_EXT"].ToString();

                    //后添加六个属性
                    //语种
                    // if (model.LANG_TYPE=="")
                    //    ddlstLANG_TYPE.SelectedValue = "1";
                    //else
                        ddlstLANG_TYPE.SelectedValue = model.LANG_TYPE.ToString();
                    //版本
                    txtBB.Text = dt.Rows[0]["FILE_VERSION"].ToString();
                    //上传时间
                    if (dt.Rows[0]["CREATE_TIME"]!="")
                        lblFILE_CRT_DATE.Text = Convert.ToDateTime(dt.Rows[0]["CREATE_TIME"].ToString()).ToString("yyyy-MM-dd");
                    //文件大小
                    txtFILE_SIZE.Text = dt.Rows[0]["FILE_SIZE"].ToString();
                    //密级
                    if (model.SECRET_LVL == "")
                        ddlstSECRET_LVL.SelectedValue = "1";
                    else
                        ddlstSECRET_LVL.SelectedValue = model.SECRET_LVL;

                    lblteamname.Visible = false;
                    lblTeam.Visible = false;
                }
            }
            else if (spacetypes[0].Equals("project"))
            {
                hidspacetype.Value = "project";
                TB_XMK_FILEInfo model = new TB_XMK_FILEInfo();
                Tb_Xmk_FileBiz bll = new Tb_Xmk_FileBiz();
                XMKFileExtInfo m = new XMKFileExtInfo();
                //DataTable dt = bll.GetModel(File_ID);
                DataTable dt = bll.GetModel(SavePath);
                if (dt.Rows.Count > 0)
                {
                    hidFile_ID.Value = dt.Rows[0]["File_ID"].ToString();
                    txtWDMC.Text = dt.Rows[0]["FILE_NAME"].ToString();
                    hidZYXX.Value = dt.Rows[0]["FILE_ABST"].ToString(); ;

                    if (dt.Rows[0]["FILE_ABST"].ToString().Length >= 10)
                    {
                        lblZYXX.Text = dt.Rows[0]["FILE_ABST"].ToString().Substring(0, 10) + " ...";
                    }
                    else
                    {
                        lblZYXX.Text = dt.Rows[0]["FILE_ABST"].ToString();
                    }
                    txtZZ.Text = dt.Rows[0]["FILE_AUTHOR"].ToString();
                    hidGJC.Value = dt.Rows[0]["KEYW"].ToString();

                    if (dt.Rows[0]["KEYW"].ToString().Length >= 10)
                    {
                        lblGJC.Text = dt.Rows[0]["KEYW"].ToString().Substring(0, 10) + " ...";
                    }
                    else
                    {
                        lblGJC.Text = dt.Rows[0]["KEYW"].ToString();
                    }

                    if (dt.Rows[0]["FILE_CRT_DATE"].ToString() == null)
                        paydate = null;
                    else
                        paydate.Value = Convert.ToDateTime(dt.Rows[0]["FILE_CRT_DATE"].ToString()).ToString("yyyy-MM-dd");
                    txtMB.Text = dt.Rows[0]["DEST_OBJ"].ToString();
                    targetValue.Value = dt.Rows[0]["DEST_OBJ"].ToString();
                    txtYWDX.Text = dt.Rows[0]["BIZ_OBJ"].ToString();
                    bizValue.Value = dt.Rows[0]["BIZ_OBJ"].ToString();
                    ddlstWDGS.SelectedValue = dt.Rows[0]["FILE_EXT"].ToString();
                    //后添加六个属性
                    //语种
                    //if (model.LANG_TYPE == "")
                    //    ddlstLANG_TYPE.SelectedValue = "1";
                    //else
                        ddlstLANG_TYPE.SelectedValue = model.LANG_TYPE.ToString();
                    //版本
                    txtBB.Text = dt.Rows[0]["FILE_VERSION"].ToString();
                    //上传时间
                    lblFILE_CRT_DATE.Text = Convert.ToDateTime(dt.Rows[0]["CREATE_TIME"].ToString()).ToString("yyyy-MM-dd");
                    //文件大小
                    txtFILE_SIZE.Text = dt.Rows[0]["FILE_SIZE"].ToString();
                    //密级
                    if (model.SECRET_LVL == "")
                        ddlstSECRET_LVL.SelectedValue = "1";
                    else
                        ddlstSECRET_LVL.SelectedValue = model.SECRET_LVL;
                    //来源课题组
                    lblTeam.Text = dt.Rows[0]["team_name"].ToString(); 
                }
            }
        }

        //绑定文档语言
        public void BindLANG_TYPE()
        {
            Tb_TeamBiz bll = new Tb_TeamBiz();
            ddlstLANG_TYPE.DataSource = bll.dtQueryCstInfo("1");
            ddlstLANG_TYPE.DataBind();
        }
        //绑定秘籍
        public void BindSECRET_LVL()
        {
            Tb_TeamBiz bll = new Tb_TeamBiz();
            ddlstSECRET_LVL.DataSource = bll.dtQueryCstInfo("5");
            ddlstSECRET_LVL.DataBind();
        }

        #endregion 函数集

       
    }
}